package advertising

import (
	"gitee.com/alex_li/amazon-rpt/internal/rest"
	"github.com/lishimeng/go-log"
)

type ProfileAccountInfo struct {
	MarketplaceId      string `json:"marketplaceStringId"`
	Id                 string `json:"id"`
	Type               string `json:"type"`
	Name               string `json:"name"`
	ValidPaymentMethod bool   `json:"validPaymentMethod"`
}

type Profile struct {
	ProfileId    int64              `json:"profileId"`
	CountryCode  string             `json:"countryCode"`
	CurrencyCode string             `json:"currencyCode"`
	Timezone     string             `json:"timezone"`
	AccountInfo  ProfileAccountInfo `json:"accountInfo"`
}

func (c *Client) GetProfile(h ...func(clientId string, profiles ...Profile)) (err error) {
	if len(h) == 0 {
		log.Info("no profile handler")
		return
	}
	handler := h[0]
	if handler == nil {
		log.Info("nil profile handler")
		return
	}
	var action = "/v2/profiles"
	var resp []Profile
	c.profileId = 0 // 清空profile
	req, err := c.request()
	if err != nil {
		log.Info(err)
		return
	}
	err = req.Path(action).Response(&resp).Accept(rest.ApplicationJson).Json(rest.GET)
	if err == nil {
		handler(c.GetConn().GetClientId(), resp...)
	}
	return
}

func (c *Client) handleProfile(profiles ...Profile) {
	if len(profiles) == 0 {
		return
	}
	var has = false
	var profile Profile
	for _, v := range profiles {
		if v.CountryCode == "US" {
			has = true
			profile = v
			break
		}
	}
	if !has {
		return
	}
	if c.onProfile != nil {
		c.onProfile(profile)
	}
	c.profileId = profile.ProfileId
}
